package com.pai.servlet;

import com.alibaba.fastjson.JSON;
import com.pai.entity.AppUser;
import com.pai.service.AppUser.AppUserService;
import com.pai.service.AppUser.AppUserServiceImpl;
import com.pai.vo.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "loginServlet",urlPatterns = {"/login"})
public class LoginServlet extends BaseServlet{
    private AppUserService theService;  //系统用户服务层

    @Override
    public void init() throws ServletException {
        super.init();
        this.theService = new AppUserServiceImpl();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();

        try{
            String action =req.getParameter("action");
            switch (action){
                case "list":
                    List<AppUser> list = theService.getList();
                    apiResult.setData(list);
                    break;
                case "detail":
                    String id = req.getParameter("id");
                    AppUser itemDetail = theService.getItemDetail(id);
                    apiResult.setData(itemDetail);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setError();
        }finally {
            write(resp, apiResult);
        }
    }

    //修改
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String jsonString = super.getJsonString(req);
        AppUser item = JSON.parseObject(jsonString, AppUser.class);

        flag = theService.update(item);

        if(!flag){
            apiResult.setCode("00001");
            apiResult.setMessage("更新失败");
        }

        super.write(resp, apiResult);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String id = req.getParameter("id");

        flag = theService.delete(id);

        if(!flag){
            apiResult.setCode("00001");
            apiResult.setMessage("删除失败");
        }

        super.write(resp, apiResult);
    }

    //登录
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        AppUser appUser = null;

        String jsonString = super.getJsonString(req);
        AppUser item = JSON.parseObject(jsonString, AppUser.class);

        try{
            appUser = theService.login(item);
            if(appUser == null){
                apiResult.setError("账号或密码错误，请重试");
            }
            else {
                HashMap<String, String> inventorys = new HashMap<>();  //键值对数据体
                inventorys.put("account", appUser.getAccount());  //登录成功，将账号传回前端，然后前端将它存储在浏览器缓存中
                apiResult.setData(inventorys);
            }
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setError();
        }finally {
            write(resp, apiResult);
        }
    }
}
